package com.zzptc.zzsgz.bdsjws.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.Toast;

import com.socks.library.KLog;
import com.zzptc.zzsgz.bdsjws.MyApplication;
import com.zzptc.zzsgz.bdsjws.services.IVersionUpdate;
import com.zzptc.zzsgz.bdsjws.services.MyDownLoadService;
import com.zzptc.zzsgz.bdsjws.utils.MyUtils;
import com.zzptc.zzsgz.bdsjws.vo.VersionInfo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/5/22.
 */

public class UpdateBrocastRecver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, final Intent intent) {
        KLog.i("--------","=====接收到了  准备更新=====");
        //访问网络，每七天更新一次 获得版本号 y用retrofir2框架访问网络
        SharedPreferences sharedPreferences= MyUtils.getpreferences();
        String olddate=sharedPreferences.getString("checkDate","2017-5-12");
        Date newdate=new Date();//当前日期
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        final String newdateformat=format.format(newdate);
        //format.format()将日期转成String
        //format.parse() 将String转成日期
        try {
           Date check =null;
            if(olddate!=null){
                check=format.parse(olddate);
            }
            //计算当前日期
            if (olddate == null || "".equals(olddate)) {
                //表示第一次安装 将当前的日期存储在共享首选项=
                SharedPreferences.Editor editor=sharedPreferences.edit();
                editor.putString("checkDate",newdateformat);
                editor.commit();
            } else {
                //计算日期差
               int days= MyUtils.daysBetween(check, newdate);
               // Toast.makeText(context, days+"", Toast.LENGTH_SHORT).show();
                if(days>=7){
                    //访问服务器  看看是否有新版本
                    Retrofit retrofit=new Retrofit.Builder()
                            .baseUrl("http://172.10.249.217:8080/")
                            .addConverterFactory(GsonConverterFactory.create())
                            .build();
                    IVersionUpdate versionUpdate=retrofit.create(IVersionUpdate.class);//创建接口对象
                    Call<VersionInfo> version=versionUpdate.getVersion();
                    //将当前日期存储在共享首选项  作为上次更新的日期
                    version.enqueue(new Callback<VersionInfo>() {
                        @Override
                        public void onResponse(Call<VersionInfo> call, Response<VersionInfo> response) {
                            //网络正常 3是网络正常可以下载 4是网络正常已经是最新版本
                            VersionInfo versionInfo= response.body();//body取到的值
                            int newversioncode=versionInfo.getVersioncode();//从服务器端获取的版本号
                            int oldversioncode=MyUtils.getVersionCode(MyApplication.getContext());//本机正在使用的版本号
                            if(newversioncode>oldversioncode){
                                //进行强制更新   启动服务
                                Intent intent1=new Intent();
                                intent1.setClass(MyApplication.getContext(), MyDownLoadService.class);
                                intent1.putExtra("aplurl",versionInfo.getApkurl());
                                MyApplication.getContext().startService(intent1);


                            }

                        }

                        @Override
                        public void onFailure(Call<VersionInfo> call, Throwable t) {

                        }
                    });

                }else {

                }
            }


        } catch (ParseException e) {
            e.printStackTrace();
        }

    }
}
